package org.seven.jrdp.web.t;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.seven.jrdp.biz.t.model.TCkd;
import org.seven.jrdp.biz.t.model.TCkdMx;
import org.seven.jrdp.biz.t.model.TCrk;
import org.seven.jrdp.biz.t.model.THt;
import org.seven.jrdp.biz.t.model.TJh;
import org.seven.jrdp.biz.t.model.TKh;
import org.seven.jrdp.biz.t.model.TWl;
import org.seven.jrdp.biz.t.model.TZz;
import org.seven.jrdp.biz.t.service.TCkdService;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.Excel2007Utils;
import org.seven.jrdp.commons.util.JsonUtils;
import org.seven.jrdp.commons.util.ServletUtils;
import org.seven.jrdp.commons.util.ShiroUtils;
import org.seven.jrdp.web.t.plugins.ExcelPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/mgt/t/ckd")
public class TCkdController {
	private static final String EXCEL_CKD = "ckd.xlsx";
	private static final String EXCEL_CKDMX = "ckdmx.xlsx";
	@Autowired
	private TCkdService tCkdService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/t/ckd/ckd_" + view);
	}

	@PostMapping("/find")
	public PageInfo<Map<String, Object>> find(PageRequest pageRequest, HttpServletRequest request) {
		Map<String, Object> map = ServletUtils.getSearchParams(request);
		PageInfo<Map<String, Object>> page = tCkdService.find(map, pageRequest);
		return page;
	}

	@RequestMapping("/create")
	public Map<String, Object> create(String ggxh, int ckzl) {
		return tCkdService.create(ggxh, ckzl);
	}

	@PostMapping("/insert")
	public Result insert(TCkd tCkd, String json_hz, String json_mx) {
		List<TCrk> tCrks = JsonUtils.getList(json_hz, TCrk.class);
		List<TCkdMx> tCkdMxs = JsonUtils.getList(json_mx, TCkdMx.class);
		tCkdService.insert(tCkd, tCrks, tCkdMxs);
		return Result.SUCCESS;
	}

	@PostMapping("/delete")
	public Result delete(String id) {
		tCkdService.delete(id);
		return Result.SUCCESS;
	}

	@RequestMapping("/wcck")
	public Result wcck(String id) {
		tCkdService.wcck(id, ShiroUtils.getUser().getId());
		return Result.SUCCESS;
	}

	@RequestMapping("/get")
	public TCkd get(String id) {
		TCkd tCkd = tCkdService.get(id);
		return tCkd;
	}

	@RequestMapping("/dy")
	public void dy(String id, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = tCkdService.getDy(id);
		Workbook workbook = Excel2007Utils.getWorkbook(ServletUtils.getExcelPath(EXCEL_CKD));
		dy(workbook, map);
		String fileName = ((TCkd) map.get("ckd")).getMx() + "出库单" + Excel2007Utils.SUFFIX;
		ServletUtils.download(workbook, fileName, request, response);
	}

	@RequestMapping("/dymx")
	public void dymx(String id, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = tCkdService.getDyMx(id);
		Workbook workbook = Excel2007Utils.getWorkbook(ServletUtils.getExcelPath(EXCEL_CKDMX));
		dymx(workbook, map);
		String fileName = map.get("mx") + "出库明细" + Excel2007Utils.SUFFIX;
		ServletUtils.download(workbook, fileName, request, response);
	}

	@SuppressWarnings("unchecked")
	private void dy(Workbook workbook, Map<String, Object> map) {
		TCkd tCkd = (TCkd) map.get("ckd");
		THt tHt = (THt) map.get("ht");
		TKh tKh = (TKh) map.get("kh");
		TJh tJh = (TJh) map.get("jh");
		TZz tZz = (TZz) map.get("zz");
		TWl tWl = (TWl) map.get("wl");
		int yckzl = (int) map.get("yckzl");
		List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
		Sheet sheet = workbook.getSheetAt(0);
		sheet.getRow(4).getCell(6).setCellValue(tHt.getHtbh());
		sheet.getRow(4).getCell(16).setCellValue(tHt.getGgxh());
		sheet.getRow(4).getCell(28).setCellValue(DateFormatUtils.format(tCkd.getDdrq(), "yyyy/MM/dd"));
		sheet.getRow(5).getCell(6).setCellValue(tHt.getHtzl());
		sheet.getRow(5).getCell(16).setCellValue(tCkd.getCkzl());
		sheet.getRow(5).getCell(28).setCellValue(tCkd.getCkzl() + tCkd.getYckzl());
		sheet.getRow(8).getCell(4).setCellValue(tKh.getMc());
		sheet.getRow(8).getCell(20).setCellValue(tKh.getDz());
		sheet.getRow(9).getCell(4).setCellValue(tKh.getDh());
		sheet.getRow(9).getCell(20).setCellValue(tKh.getCz());
		sheet.getRow(12).getCell(0).setCellValue(tJh.getMc());
		sheet.getRow(12).getCell(20).setCellValue(tJh.getFzr());
		sheet.getRow(12).getCell(25).setCellValue(tJh.getDh());
		sheet.getRow(14).getCell(0).setCellValue(tZz.getMc());
		sheet.getRow(14).getCell(20).setCellValue(tZz.getDz());
		sheet.getRow(15).getCell(4).setCellValue(tZz.getDh());
		sheet.getRow(15).getCell(20).setCellValue(tZz.getCz());
		sheet.getRow(16).getCell(4).setCellValue(tZz.getZclxr());
		sheet.getRow(16).getCell(9).setCellValue(tZz.getZcsj());
		sheet.getRow(16).getCell(20).setCellValue(tZz.getPzfzr());
		sheet.getRow(16).getCell(25).setCellValue(tZz.getPzsj());
		sheet.getRow(18).getCell(4).setCellValue(tWl.getMc());
		sheet.getRow(18).getCell(23).setCellValue(tWl.getYsfs());
		sheet.getRow(19).getCell(4).setCellValue(tWl.getFzr());
		sheet.getRow(19).getCell(9).setCellValue(tWl.getSj());
		sheet.getRow(20).getCell(23).setCellValue(DateFormatUtils.format(tCkd.getCkrq(), "yyyy/MM/dd"));
		sheet.getRow(21).getCell(23).setCellValue(DateFormatUtils.format(tCkd.getDdrq(), "yyyy/MM/dd"));
		int row = 27;
		sheet.getRow(row).getCell(1).setCellValue(tHt.getGgxh());
		int zzl = 0;
		int zks = 0;
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> hwmx = list.get(i);
			sheet.getRow(row).getCell(8).setCellValue((String) hwmx.get("cpbh"));
			sheet.getRow(row).getCell(14).setCellValue((Integer) hwmx.get("zl"));
			sheet.getRow(row).getCell(19).setCellValue((Integer) hwmx.get("ks"));
			zzl += (Integer) hwmx.get("zl");
			zks += (Integer) hwmx.get("ks");
			++row;
			if (row > 33) {
				break;
			}
		}
		sheet.getRow(34).getCell(14).setCellValue(zzl);
		sheet.getRow(34).getCell(19).setCellValue(zks);
		sheet.getRow(34).getCell(23).setCellValue(yckzl);
	}

	@SuppressWarnings("unchecked")
	private void dymx(Workbook workbook, Map<String, Object> map) {
		ExcelPlugin plugin = new ExcelPlugin(workbook);
		Sheet sheet = workbook.getSheetAt(0);
		sheet.getRow(0).getCell(0).setCellValue(map.get("year") + " 年铝锭出货明细表（" + map.get("ggxh") + "）");
		sheet.getRow(3).getCell(11).setCellValue((String) map.get("mx"));
		sheet.getRow(64).getCell(3).setCellValue((Integer) map.get("zl"));
		List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
		int row = 4;
		int index = 0;
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> mx = list.get(i);
			sheet.getRow(row + i).getCell(0).setCellValue(i + 1);
			sheet.getRow(row + i).getCell(3).setCellValue((String) mx.get("cpbh"));
			sheet.getRow(row + i).getCell(4).setCellValue((String) mx.get("cpxh"));
			sheet.getRow(row + i).getCell(5).setCellValue((Integer) mx.get("zl"));
			if (i == 0) {
				Map<String, Object> hz = (Map<String, Object>) map.get((String) mx.get("cpbh"));
				index = row + i;
				sheet.getRow(row + i).getCell(11).setCellValue(new XSSFRichTextString(hz.get("zl") + "kg\n" + hz.get("ks") + "捆"));
			} else {
				if (!((String) mx.get("cpbh")).equals((String) list.get(i - 1).get("cpbh"))) {
					if (index != row + i - 1) {
						sheet.addMergedRegion(new CellRangeAddress(index, row + i - 1, 11, 11));
					}
					sheet.getRow(row + i - 1).getCell(1).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(2).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(3).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(4).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(5).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(6).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(7).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(8).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(9).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(10).setCellStyle(plugin.STYLE_FONT8);
					sheet.getRow(row + i - 1).getCell(11).setCellStyle(plugin.STYLE_FONT8);
					Map<String, Object> hz = (Map<String, Object>) map.get((String) mx.get("cpbh"));
					index = row + i;
					sheet.getRow(row + i).getCell(11).setCellValue(new XSSFRichTextString(hz.get("zl") + "kg\n" + hz.get("ks") + "捆"));
				}
			}
		}
		if (index != row + list.size() - 1) {
			sheet.addMergedRegion(new CellRangeAddress(index, row + list.size() - 1, 11, 11));
		}
	}
}